import Dialog from '@vant/weapp/dialog/dialog';

Page({
  data: {
    dice: ['first', 'second', 'third', 'fourth', 'fifth', 'sixth'],
    buttonType: 'primary',
    stopType: 'warn',
    buttonValue: 'shake', 
    isShow: 'show',     //是否显示shake键
    num1: 0,
    num2: 0,
    num3: 0,
    num4: 0,
    num5: 0,
    x: 0,
    y: '1290rpx',           //图像偏移量
    animation: 0,    //动画效果 0不显示1显示
  },
  shakeClick() {
    //盖盖子
    this.setData({
      x: 0,
      y: '1290rpx',
      animation: 1,
    });

    let timer = null;
    let timeout = 2400;
    this.addmusic('/images/shake.wav',timeout);
    let me = this;
    timer = setInterval(function () {
      let num1 = Math.floor(Math.random() * 6);
      let num2 = Math.floor(Math.random() * 6);
      let num3 = Math.floor(Math.random() * 6);
      let num4 = Math.floor(Math.random() * 6);
      let num5 = Math.floor(Math.random() * 6);
      me.setData({ num1: num1 });
      me.setData({ num2: num2 });
      me.setData({ num3: num3 });
      me.setData({ num4: num4 });
      me.setData({ num5: num5 });
    }, 50);

    setTimeout(()=>{
      clearInterval(timer);
      this.setData({
        animation: 0
      })
    }, timeout);
    this.setData({
      dice: this.data.dice,
      isShow: 'hidden',
    });

  },
  addmusic(musicSrc,timeout) {
    //添加音效
    const innerAudioContext = wx.createInnerAudioContext()
    innerAudioContext.autoplay = true  // 是否自动开始播放，默认为 false
    innerAudioContext.loop = true  // 是否循环播放，默认为 false
    wx.setInnerAudioOption({ // ios在静音状态下能够正常播放音效
      obeyMuteSwitch: false,   // 是否遵循系统静音开关，默认为 true。当此参数为 false 时，即使用户打开了静音开关，也能继续发出声音。
      success: function (e) { },
      fail: function (e) { }
    })
    innerAudioContext.src = musicSrc;  // 音频资源的地址
    innerAudioContext.onPlay(() => {  // 监听音频播放事件
    })
    innerAudioContext.onError((res) => { // 监听音频播放错误事件
      wx.showToast({
        title: '播放声音出错啦！',
        icon: 'none',
        duration: 1500,
        mask: false
      });
    })

    setTimeout(() => {
      innerAudioContext.stop();
    }, timeout);
  },
  restartClick() {
    this.addmusic('/images/restart.wav',1100);
    this.setData({
      isShow: "show"
    });
  },
  
  onShow() {  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: '快来和帅哥摇骰子喝酒!!!',
      path: '/pages/dice/dice',
      // 设置转发的图片
      imageUrl: 'https://mp-f3b68706-562e-4e88-9436-5e8c05efc576.cdn.bspapp.com/cloudstorage/7ab9bb73-6f8d-4e85-8b9e-983561662fed.png',
      // 成功的回调
      success: (res) => {
        wx.showToast({
          title: '分享成功！',
          icon: 'success',
          duration: 1500,
        });
      },
      // 失败的回调
      fail: (res) => { },
      // 无论成功与否的回调
      complete: (res) => { }
    }
  }
})
